<?php
/**
 * Created by PhpStorm.
 * User: zzw
 * Date: 17-3-10
 * Time: 下午4:35
 */
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Concern_fans_model extends MY_Model {

    protected $table = 'concern_fans';

    /**
     * @param $uids
     * @param $rids
     * @return mixed
     * 新朋友相似度
     */
    public function get_similarity_uids($uids,$rids) {
        $query = $this->db->select('uid')
            ->from($this->table)
            ->where(array('in'=>array('uid',$uids),'in 2'=>array('rid',$rids),'is_deleted'=>1))
            ->group_by('uid')
            ->get();
        $ids = array();
        foreach($query->result_array() as $key=>$row) {
            $ids[$row['uid']]['id'] = $row['uid'];
            $ids[$row['uid']]['title'] = '相似度推荐';
            //$ids[] = $row['uid'];
        }
        return $ids;
    }

    /**
     * @return mixed
     * 最新粉丝
     */
    public function get_new_nums($uid){
        $query = $this->db
            ->select('count(*) as total')
            ->from($this->table)
            ->where(array('is_new'=>1,'rid'=>$uid,'is_deleted'=>1))
            ->get();
        return $query->row_array();
    }

    //通讯录好友
    //行业活跃:所在行业粉丝数前10、各个感兴趣行业粉丝数前10
    //最火的人：平台上粉丝数前10
    public function get_phone_ids($phone,$user,$similarity){

        $phone_ids = array();
        $hot_ids = array();
        $industry_ids = array();
        if($phone) {
            $phone = explode(',',$phone);
            $query = $this->db->select('id')
                ->from('account')
                ->where(array('in'=>array('phone',$phone),'audit'=>0,'status'=>2,'id <>'=>$user['id']))
                ->get();
            foreach($query->result_array() as $key=>$row) {
                $phone_ids[$row['id']]['id'] = $row['id'];
                $phone_ids[$row['id']]['title'] = '通讯录好友';
            }
        }
        switch($user['ctype']) {
            case 1:
                //$cids = array(24,25);
                $parm = array('industry');
                $data = $user['industry'] ? explode(',',$user['industry']) : '';
                break;
            case 2:
                //$cids = array(24,26,27);
                $parm = array('industry');
                $data = $user['industry'] ? explode(',',$user['industry']) : '';
                break;
            case 3:
                //$cids = array(24,24);
                $parm = array('industry','interested');
                $arr1 = $user['industry'] ? explode(',',$user['industry']) : '';
                $arr2 = $user['interested'] ? explode(',',$user['interested']) : '';
                $data = array_unique(array_merge($arr1,$arr2));
                break;
            case 4:
                //$cids = array(24,24);
                $parm = array('industry','interested');
                $arr1 = $user['industry'] ? explode(',',$user['industry']) : '';
                $arr2 = $user['interested'] ? explode(',',$user['interested']) : '';
                $data = array_unique(array_merge($arr1,$arr2));
                break;
            case 5:
                //$cids = array(24);
                $parm = array('interested');
                $data = $user['interested'] ? explode(',',$user['interested']) : '';
                break;
            case 6:
                //$cids = array(28,24);
                $parm = array('interested');
                $data = $user['interested'] ? explode(',',$user['interested']) : '';
                break;
        }
        $hangyeids = array();
        if($data) {
            $where = array('account_info.uid <> '=>$user['id'],'account.audit'=>0,'account.status'=>2);
            if(count($parm) == 2) {
                $where_in = array('in'=>array('account_info.'.$parm[0],$data),'or_in'=>array('account_info.'.$parm[1],$data));
            } else {
                $where_in = array('in'=>array('account_info.'.$parm[0],$data));
            }
            $where = array_merge($where,$where_in);
            $industry = $this->db->select('account_info.uid')
                ->from('account_info')
                ->join('account','account.id=account_info.uid','left')
                ->where($where)
                ->order_by('account.fans','desc')
                ->limit(10,0)
                ->get();
            //var_dump($industry->result_array());
            foreach($industry->result_array() as $key=>$industries) {
                $hangyeids[] = $industries['uid'];
                $industry_ids[$industries['uid']]['id'] = $industries['uid'];
                $industry_ids[$industries['uid']]['title'] = '行业活跃';
                //$industry_ids[] = $industries['id'];
            }
        }

        //最火的人：平台上粉丝数前10
        $actives = $this->db->select('id')
            ->from('account')
            ->where(array('id <> '=>$user['id'],'audit'=>0,'status'=>2))
            ->order_by('fans','desc')
            ->limit(10,0)
            ->get();
        foreach($actives->result_array() as $key=>$val) {
            if($hangyeids && in_array($val['id'],$hangyeids)) continue;
            $hot_ids[$val['id']]['id'] = $val['id'];
            $hot_ids[$val['id']]['title'] = 'HOT';
            //$hot_ids[] = $val['id'];
        }
        //$ids  = array_unique(array_merge($phone_ids,$similarity,$industry_ids,$hot_ids));
        return array_merge($phone_ids,$similarity,$industry_ids,$hot_ids);
    }
    //只保留通讯录好友
    public function get_phone_id($phones,$uphone) {
        $phone_ids = array();
        if($phones) {
            $phones = explode(',',$phones);

            $phone = array();
            foreach($phones as $row) {
                if($row == $uphone) continue;
                $phone[] = $row;
            }

            if($phone) {
                $query = $this->db->select('id')
                    ->from('account')
                    ->where(array('in'=>array('phone',$phone),'audit'=>0,'status'=>2))
                    ->get();
                foreach($query->result_array() as $key=>$row) {
                    $phone_ids[$row['id']]['id'] = $row['id'];
                    $phone_ids[$row['id']]['title'] = '通讯录好友';
                }
            }
        }
        return $phone_ids;
    }
}